<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<form id="vote_add5" style="margin:0;padding:5px 0 0 25px;color:#333;">

	<p >投票标题：<input type="text" name1="vote_title" class="textbox" style="width:500px;"> <button id="btn_add"  onClick="vote_title_add5(this)">增加一题</button></p>
	<p name1='vo_title'>题目<span name1='num'></span>:<input type="text" name1="title" class="textbox" style="width:500px;">类型:
	 <select  name1="vote_type" style="width:100px;" onchange="changetype(this)">
	   <option value="" selected='true' >请选择</option>
       <option value="radio">单选题</option>
       <option value="checkbox">多选题</option>
       <option value="text">填空题</option>
     </select>
	</p>
	
	<div vo_test='test1'></div>
</form>
<div align="center">
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick=add_new_vote()>Save</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">cancel</a>
</div>
		


<script type="text/javascript">



function add_new_vote(){
    var p1=$("p[name1='vo_title']") ;
    var str3 ="";
    for(var i=1;i<p1.length+1;i++){
       var title = p1.eq(i-1).find("input[name1='title']").val() ;
       var parent_order = i ;
       var vote_type =p1.eq(i-1).find("select[name1='vote_type']").val() ;
       var str2 ="";
       var str1 ="" ;
       
       var max_checkedCount = p1.eq(i-1).find("select[name1='max_checkedCount']").val() ;
       if(typeof(max_checkedCount) == "undefined"){
          max_checkedCount = 0 ;
       }
       var input = p1.eq(i-1).next().find("input[name1='checkbox']");
       
/*        console.log(title+"|"+parent_order+"|"+vote_type+"|"+max_checkedCount+"|") ;
       console.log(input); */
       for(var j=1;j<input.length+1;j++){
          checkbox = input.eq(j-1).val() ;
          str2 +='checkbox_'+j+':'+ checkbox +',' ;
       
       }
       str1 = 'parent_order :'+parent_order+','+'title:'+'\''+title+'\''+','+ str2 +'vote_type :'+vote_type+','+ 'max_checkedCount :'+max_checkedCount ;
       str3 +='{'+str1+'},' ;
       
    }
     var str4 = '['+str3+']' ;
     /* var Josnstr4 = ; */
     
     console.log(str4) ; 
     
     var votelist1 =[{'name':'jim' , 'age':20} , {'name':'king' , 'age':26},{'name':'jge' , 'age':30}] ;
     var votelist = [{'parent_order' :1,'title':'4324','checkbox_1':'4324','checkbox_2':'324324','checkbox_3':'32432','checkbox_4':'432432','vote_type' :'radio','max_checkedCount' :0}] ;
     
     var voteArray = new Array();
     voteArray.push({parent_order:'1',title:'22222',checkbox_1:'3333333',checkbox_2 : '44444'})
     voteArray.push({parent_order:'2',title:'26662222',checkbox_1:'3333366633',checkbox_2 : '44666444'})
                 $.ajax({
                url: "vote/vote_add",
                type: "POST",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                dataType:"json",
                data: JSON.stringify(voteArray),    //将Json对象序列化成Json字符串，JSON.stringify()原生态方法
                //data: $.toJSON(voteArray),            //将Json对象序列化成Json字符串，toJSON()需要引用jquery.json.min.js
               /*  success: function(data){
                    alert(data);
                },
                error: function(res){
                    alert(res.responseText);
                } */
            });
    /* $.post("vote/vote_add",{votelist : votelist},
        function(){
        alert("11") ;
        }
        );  */
     
     /*  $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json", 
                url: "vote/vote_add",//传入后台的地址/方法
                data: {votelist : votelist},//参数，这里是一个json语句
                 success: function (data) {
                    var result = data.d;
                    alert(result);
                },
                error: function (err) {
                    alert("err:" + err);
                }
            });  */
     
}

function changetype(select){
   var div = $(select).parent().next() ;
   var select_val = $(select).val() ;
   var select_next = "<strong>最多可选：<select class='easyui-combobox' name1='max_checkedCount' style='width:60px;' ><option value='0' selected='true' >请选择</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></strong>" ;
   var div_html = "<p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp选项<span name1='num2'></span>：<input name1='checkbox' class='textbox'  style='width:400px;'></p><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp选项<span name1='num2'></span>：<input name1='checkbox' class='textbox'  style='width:400px;'></p><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp选项<span name1='num2'></span>：<input name1='checkbox' class='textbox'  style='width:400px;'></p><p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp选项<span name1='num2'></span>：<input name1='checkbox' class='textbox'  style='width:400px;'> <button id='aass' onClick='voteanswer_add5(this)'>增加选项</button><button  onClick='voteanswer_del(this)'>删除</button></p>" ;
   var btn_html = ""
  switch(select_val)
   {
   case "radio" :
   $(select).next().remove() ;
   div.html(div_html) ;
	break;
   case "checkbox" :
   $(select).next().remove() ;
   $(select).after(select_next);
   div.html(div_html) ;
	break;
	case "text" :
	$(select).next().remove() ;
	div.html("") 
   default:
   	
   } 
   
 }
 function vote_title_add5(btn){
   var btn = $(this) ; 
   var temp1 = "<p>题&nbsp&nbsp&nbsp目：<input type='text' name='title' class='textbox' style='width:500px;'/> 类&nbsp&nbsp&nbsp型：<select class='easyui-combobox' name='vote_type' style='width:100px;' onchange='changetype(this)'><option value='' selected='true' >请选择</option><option value='radio'>单选题</option><option value='checkbox'>多选题</option><option value='text'>填空题</option></select><div name='div1'></div>" ;
   var temp= "<p name1='vo_title'>题目<span name1='num'></span>:<input type='text' name1='title'  style='width:500px;'/>类型:<select class='easyui-combobox' name1='vote_type' style='width:100px;' onchange='changetype(this)'><option value='' selected='true' >请选择</option><option value='radio'>单选题</option><option value='checkbox'>多选题</option><option value='text'>填空题</option></select></p><div vo_test='test1'></div>"
     $("div[vo_test='test1']").last().after(temp);  
     var p = $("p[name1='vo_title']") ;
     /* p.eq(0).html().replace("题目","题目1") ; */
    /*  var num = {"一","二","三","四","五","六"} ; */
     for(var i=1;i<p.length+1;i++){
        /* p.eq(i-1).html().replace("题目","题目"+i) ; */
       /*  console.log(p.eq(i-1).html().replace("题目","题目"+i));
        p.eq(i).html(p.eq(i-1).html().replace("题目","题目"+i)) ; */
        $("span[name1='num']").eq(i-1).html(i);
    } 
   
 }
   function voteanswer_add5(bb){
       var btn =$(bb) ;
       var temp = "<p>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp选项<span name1='num2'></span>：<input name1='checkbox' class='textbox'  style='width:400px;'> <button  onClick='voteanswer_add5(this)'>增加选项</button><button  onClick='voteanswer_del(this)'>删除</button></p>"
       btn.parent().after(temp) ;
       var span = btn.parent().parent().find("span[name1='num2']")
       for(var i=1;i<span.length+1;i++){
           span.eq(i-1).html(i);
       }
   }
    function voteanswer_del(bb){
        var btn=$(bb) ;
        var p = btn.parent().parent()
        btn.parent().remove() ;
        var span = p.find("span[name1='num2']") ;
        for(var i=1;i<span.length+1;i++){
           span.eq(i-1).html(i);
           
       }
    }
 
 </script>